import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:xvideos/alert_business/splash/no_permission_dialog.dart';

class PermissionUtil {
  static Future<bool> checkPhotoPermission() async {
    AndroidDeviceInfo androidInfo = await DeviceInfoPlugin().androidInfo;
    String androidVersion = androidInfo.version.release;

    PermissionStatus status;

    if (int.parse(androidVersion) < 13) {
      status = await Permission.storage.request();
    } else {
      status = await Permission.photos.request();
    }
    if (!status.isGranted) {
      NoPermissionDialog.show().then((value) {
        if (value == true) openAppSettings();
      });
      return false;
    }
    return true;
  }

  static Future checkPermissionAlways() async {
    if (!Platform.isAndroid) return;
    while (true) {
      var status = await canReadStorage();
      if (status.isGranted) return;
      // 展示无权限，去设置的对话框
      var val = await NoPermissionDialog.show();
      if (val == true) {
        // 跳转到应用配置界面
        var isCanOpen = await openAppSettings();
      }
    }
  }

  static Future<PermissionStatus> canReadStorage() async {
    AndroidDeviceInfo androidDeviceInfo = await DeviceInfoPlugin().androidInfo;
    String androidVersion = androidDeviceInfo.version.release;
    late PermissionStatus status;
    if (int.parse(androidVersion) <= 12) {
      status = await Permission.storage.request();
    } else {
      status = await Permission.photos.request();
    }
    return status;
  }

  //申请相机权限
  static Future<PermissionStatus> checkCameraStorage() async {
    late PermissionStatus status;
    status = await Permission.camera.request();
    return status;
  }

  static Future<bool> checkCameraPermission() async {
    PermissionStatus status = await Permission.camera.request();
    if (!status.isGranted) {
      NoPermissionDialog.show().then((value) {
        if (value == true) openAppSettings();
      });
      return false;
    }
    return true;
  }
}
